package com.dodoke.cake.controller;

import com.alibaba.fastjson.JSON;
import com.dodoke.cake.bean.Classification;

import com.dodoke.cake.service.CatalogService;
import com.dodoke.cake.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CatalogController {
    @Autowired
    private CatalogService cs;
    @RequestMapping("/catalog_list")
    public String catalog_list(){
        return "catalog_list";
    }
    @RequestMapping("/catalog_add")
    public String catalog_add(){
        return "catalog_add";
    }

    /**
     * 获取分类
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCatalog")
    public Result CatalogListPageData() {
        List<Classification> catalogs = cs.getAllData();
        System.out.println(catalogs);
        Result res = new Result();
        res.result = catalogs;
        return res;
    }

    /**
     * 单个删除分类
     */
    @RequestMapping("/catalogDelete")
    @ResponseBody
    public String catalogDelete(@RequestBody Map<String, String> map) {
        System.out.println(map.get("id"));
        this.cs.deleteCatalog(Integer.parseInt(map.get("id")));
        return "";
    }

    /**
     *批量删除分类
     */
    @RequestMapping("/catalogBatchDeleyte")
    @ResponseBody
    public String catalogBatchDeleyte(@RequestBody Map<String, List<String>> map) {
        List<String> list = map.get("ids");
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i));
            this.cs.deleteCatalog(Integer.parseInt(list.get(i)));
        }
        return "";
    }

    /**
     * 增加分类
     * @return
     */
    @RequestMapping("/addCatalog")
    public String addCatalog(HttpServletRequest request) {
        System.out.println(request.getParameter("title"));
        Classification cf = new Classification();
        String[][] s =new String[100][];
        String[] catalogName= request.getParameterValues("title");
        s[0] = catalogName;
        String[] selValues = request.getParameterValues("pid");
        s[1] = selValues;
        String[] parentName = request.getParameterValues("info");
        s[2] = parentName;
        for(int i = 0;i<s[0].length;i++) {
            System.out.println(s[1][i]);
            cf.setCatalogName(s[0][i]);
            cf.setParentId(this.cs.getIdByTypeName(s[2][i]));
            if(this.cs.getLevelIdByTypeName(s[1][i]) == 2) {
                cf.setLevelId(3);
            } else if(this.cs.getLevelIdByTypeName(s[1][i]) == 3){
                cf.setLevelId(3);
            }else if(this.cs.getLevelIdByTypeName(s[1][i]) == 1) {
                cf.setLevelId(2);
            }else if(s[1][i].equals("蛋糕主类")) {
                cf.setLevelId(1);
                cf.setParentId(0);
            }
            this.cs.addCatelogList(cf);
            //flag = this.cl.addCatelogList(cf);
        }

        /*String catalogNamestr = map.get("catalogName");
        System.out.println(catalogNamestr);
        String[] catalogName = catalogNamestr.split("===");
        String parentStr = map.get("parentName");
        String[] parentId = parentStr.split("===");
        String desStr = map.get("des");
        String[] description = desStr.split("===");
        int[] parentId2 = new int[parentId.length];
        for(int i = 0; i < parentId.length; i++) {
            parentId2[i] = Integer.parseInt(parentId[i]);
        }
        for(int i = 0; i < catalogName.length; i++) {
            int levelId = 0;
            levelId = cs.selectLevelId(parentId2[i]) + 1;
            cs.addCatalog(catalogName[i],levelId,parentId2[i],description[i]);
        }*/
        return "catalog_list";
    }
}
